//
// Created by antler on 2018/12/19.
//

#include "UIView.h"

#include "GlobalMacros.h"

#include <string>

#include "log.h"

#include "UIViewUtils.h"

UIView::UIView()
        : mUIViewObj(nullptr) {

}

UIView::~UIView() {
    removeAllActionCallback();
    releaseUIViewObject();
}

bool UIView::initWith(jobject juiview) {
    LOGV("UIView::initWith uiview:%p", juiview);
    setUIViewObject(juiview);
    return true;
}

std::string UIView::getID() {
    return UIViewUtils::getID(mUIViewObj);
}

int UIView::getType() {
    return UIViewUtils::getType(mUIViewObj);
}

bool UIView::getAttr_Bool(const std::string &key, bool def) {
    return false;
}

int UIView::getAttr_Int(const std::string &key, int def) {
    return 0;
}

std::string UIView::getAttr_String(const std::string &key, const std::string &def) {
    return std::string();
}

std::string UIView::getAttrs() {
    return std::string();
}

bool UIView::getStyle_Bool(const std::string &key, bool def) {
    return false;
}

int UIView::getStyle_Int(const std::string &key, int def) {
    return 0;
}

std::string UIView::getStyle_String(const std::string &key, const std::string &def) {
    return std::string();
}

std::string UIView::getStyles() {
    return std::string();
}

bool UIView::getPseudoStyle_Bool(const std::string &key, bool def) {
    return false;
}

int UIView::getPseudoStyle_Int(const std::string &key, int def) {
    return 0;
}

std::string UIView::getPseudoStyle_String(const std::string &key, const std::string &def) {
    return std::string();
}

std::string UIView::getPseudoStyles() {
    return std::string();
}

void UIView::setAttr(const std::string &attrs) {
    LOGV("UIView::setAttr \nattrs:%s", attrs.c_str());
    UIViewUtils::setAttr(mUIViewObj, attrs);
}

void UIView::setStyle(const std::string &styles) {
    LOGV("UIView::setStyle \nattrs:%s", styles.c_str());
    UIViewUtils::setStyle(mUIViewObj, styles);
}

void UIView::setPseudoStyle(const std::string &pseudo, const std::string &styles) {
    LOGV("UIView::setPseudoStyle \npseudo:%s\nstyles:%s", pseudo.c_str(), styles.c_str());
    UIViewUtils::setPseudoStyle(mUIViewObj, pseudo, styles);
}

int UIView::subviewsCount() {
    int subviewsCount = 0;

    do {
        if (!isOk()) {
            break;
        }

        subviewsCount = UIViewUtils::subviewsCount(mUIViewObj);
    } while (0);
    return subviewsCount;
}

UIView *UIView::getSubview(int index) {
    UIView *subview = nullptr;

    do {
        if (!isOk()) {
            break;
        }

        jobject juisubview = UIViewUtils::getSubview(mUIViewObj, index);
        if (!juisubview) {
            break;
        }

        subview = new(std::nothrow) UIView();
        if (!subview) {
            break;
        }

        subview->initWith(juisubview);
    } while (0);
    return subview;
}

void UIView::addSubview(UIView *subview, int index) {
    do {
        if (!isOk()) {
            break;
        }

        if (!subview || !subview->isOk()) {
            break;
        }

        UIViewUtils::addSubview(mUIViewObj, subview->getUIViewObject(), index);
    } while (0);
}

void UIView::removeSubview(int index) {
    do {
        if (!isOk()) {
            break;
        }

        UIViewUtils::removeSubview(mUIViewObj, index);
    } while (0);
}

void UIView::removeFromParent() {
    do {
        if (!isOk()) {
            break;
        }

        UIViewUtils::removeFromParent(mUIViewObj);
    } while (0);
}

void UIView::setActionCallback(int action, int funID) {
    do {
        if (!isOk()) {
            break;
        }

        UIViewUtils::setActionCallback(mUIViewObj, action, funID);
    } while (0);
}

std::string UIView::toJson() {
    std::string json;
    do {
        if (!isOk()) {
            break;
        }

        json = UIViewUtils::toJson(mUIViewObj);
    } while (0);
    return json;
}

bool UIView::isOk() {
    return mUIViewObj != nullptr;
}

jobject UIView::getUIViewObject() {
    return mUIViewObj;
}

void UIView::setUIViewObject(jobject juiview) {
    releaseUIViewObject();

    JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
    if (!env) {
        return;
    }

    if (juiview) {
        mUIViewObj = env->NewGlobalRef(juiview);
    }
}

void UIView::releaseUIViewObject() {
    JNIEnv *env = LuaJavaHelper::getInstance()->getEnv();
    if (!env) {
        return;
    }

    if (isOk()) {
        env->DeleteGlobalRef(mUIViewObj);
        mUIViewObj = nullptr;
    }
}

void UIView::removeAllActionCallback() {
    do {
        if (!isOk()) {
            break;
        }

        UIViewUtils::removeAllActionCallback(mUIViewObj);
    } while (0);
}


